#define  _CRT_SECURE_NO_WARNINGS
#include "Heap.h"

void CreatData()
{
	int n = 100;
	srand((unsigned int)time(NULL));
	FILE* fin = fopen("data.txt", "w");
	if (fin == NULL)
	{
		perror("fin fail\n");
		return;
	}
	int i = 0;
	for (i = 0; i < n; i++)
	{
		int ret = rand() % n;
		fprintf(fin, "%d\n", ret);
	}
	fclose(fin);
	fin = NULL;
}

void PrintTopk(int k)
{
	FILE* fout = fopen("data.txt", "r");
	if (fout == NULL)
	{
		perror("fopen fail\n");
		return;
	}
	int* arr = (int*)malloc(k * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	int i = 0;
	for (i = 0; i < k; i++)
	{
		fscanf(fout, "%d", &arr[i]);
	}
	for (i = 0; i < k; i++)
	{
		AdjustUp(arr, i);
	}
	int ret = 0;
	while (!feof(fout))
	{
		fscanf(fout, "%d", &ret);
		if (ret > arr[0])
		{
			arr[0] = ret;
			AdjustDown(arr, k, 0);
		}
	}
	int end = k;
	while (end)
	{
		Swap(&arr[0], &arr[end - 1]);
		end--;
		AdjustDown(arr, end, 0);
	}
	
	for (int i = 0; i < k; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	fclose(fout);
	fout = NULL;
}

int main()
{
	//CreatData();
	PrintTopk(5);
	return 0;
}